﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NutLog;
using NutClass.utils;

namespace Nut.utils
{
    public class FileCookie: UtilsHelper
    {
        public static void CreateDirYes(String dir)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }
        /// <summary>
        /// 删除目录及其子目录和文件
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static bool DeleteDir(string Path)
        {
            bool rs = false;
            try
            {
                DirectoryInfo di = new DirectoryInfo(Path);
                di.Delete(true);
                return true;
            }
            catch (Exception e)
            {
                LogHelp.trace(typeof(FileCookie), e.Message);
            }

            return rs;

        }
        public static void DeleteDirectory(string dir)
        {
            if (Directory.GetDirectories(dir).Length == 0 && Directory.GetFiles(dir).Length == 0)
            {
                Directory.Delete(dir);
                return;
            }
            foreach (string var in Directory.GetDirectories(dir))
            {
                DeleteDirectory(var);
            }
            foreach (string var in Directory.GetFiles(dir))
            {
                File.Delete(var);
            }
            Directory.Delete(dir);
        }
        /// <summary>
        /// 清空目录
        /// </summary>
        /// <param name="dir"></param>
        public static void EmptyDirectory(string dir)
        {
            if (Directory.GetDirectories(dir).Length == 0 && Directory.GetFiles(dir).Length == 0)
            {
                Directory.Delete(dir);
                return;
            }
            foreach (string var in Directory.GetDirectories(dir))
            {
                DeleteDirectory(var);
            }
            foreach (string var in Directory.GetFiles(dir))
            {
                File.Delete(var);
            }

        }


        /// <summary>
        /// 是否为非空目录 true=非空，false=空目录 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool HaveFilesOrDirectory(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            if (dir.Exists)
            {
                DirectoryInfo[] childs = dir.GetDirectories();

                if (childs.Length > 0)
                {
                    return true;
                }
                FileInfo[] fs = dir.GetFiles();
                if (fs.Length > 0)
                {
                    return true;
                }
            }

            return false;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="file1"></param>
        /// <param name="file2"></param>
        /// <param name="overwrite"></param>
        public static void Copy(String file1, String file2, Boolean overwrite)
        {
            try
            {
                if (!File.Exists(file1)) return;
                File.Copy(file1, file2, overwrite);



            }
            catch (Exception ex)
            {
                LogMessage?.Invoke(ex);
            }


        }

        public static void Copy(String dir1, String file1, String dir2, String file2, Boolean overwrite)
        {

            if (!Directory.Exists(dir1)) Directory.CreateDirectory(dir1);
            if (!Directory.Exists(dir2)) Directory.CreateDirectory(dir2);

            Copy(dir1 + "\\" + file1, dir2 + "\\" + file2, overwrite);
        }
        public static void Copy(String file1, String dir2, String file2, Boolean overwrite)
        {


            if (!Directory.Exists(dir2)) Directory.CreateDirectory(dir2);

            Copy(file1, dir2 + "\\" + file2, overwrite);
        }
        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcdir">如 c:/temp/123</param>
        /// <param name="desdir">如 d:/temp</param>
        public  static void  CopyDirectory(string srcdir, string desdir,bool overwrite)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            string desfolderdir = desdir + "\\" + folderName;

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {

                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir, overwrite);
                }

                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;


                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }


                    File.Copy(file, srcfileName, overwrite);
                }
            }//foreach 
        }


    }
}
